﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace MassiveBattle.Manager.Editor
{
	[CustomEditor(typeof(BaseBossManager), true)]
	public class BaseBossManagerEditor : BaseManagerEditor
	{
		private Controller.Boss.BaseBossController controller;
		private IList<MethodInfo> actionFuncList;

		protected override void ShowTester(BaseManager inst, BaseManager.CheatData cheat)
		{
			if (EditorApplication.isPlaying && false)
			{
				if (controller == null)
				{
					if (inst.DataList[ESide.ENEMY].CharacterList.Count <= 0)
						return;

					controller = inst.DataList[ESide.ENEMY].CharacterList[0] as Controller.Boss.BaseBossController;

					var type = controller.GetType();
					var methodList = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

					actionFuncList = methodList.Where(data => data.Name.StartsWith("Execute_")).ToList();
				}

				ShowLabelField("테스트");
				Indent();
				var phase = EditorGUILayout.IntPopup("페이즈", inst.Phase, new[] { "1", "2" }, new[] { 1, 2 });
				if (phase != inst.Phase)
					controller.ChangePhase(phase);
				foreach (var actionFunc in actionFuncList)
				{
					string name = null;

					if (!actionFunc.Name.Contains("Phase" + inst.Phase))
						continue;

					if (actionFunc.Name.Contains("Attack"))
						name = "일반 공격";
					else if (actionFunc.Name.Contains("Skill"))
						name = "특수 공격";

					if (name == null)
						continue;
					EditorGUILayout.BeginHorizontal();
					EditorGUILayout.LabelField(name + " : ");
					if (GUILayout.Button("재생"))
						actionFunc.Invoke(controller, null);
					EditorGUILayout.EndHorizontal();
				}
				ShowTake(EMassTake.enterstage01, new[] { controller.ModelGameObject });
				ShowTake(EMassTake.idle01, new[] { controller.ModelGameObject });
				ShowTake(EMassTake.idle02, new[] { controller.ModelGameObject });
				ShowTake(EMassTake.down01, new[] { controller.ModelGameObject });
				Outdent();
				EditorGUILayout.Space();
			}
		}

		private void ShowTake(EMassTake take, UnityEngine.Object[] actorList, bool isLoop = false, EArea area = EArea.NONE)
		{
			EditorGUILayout.BeginHorizontal();
			EditorGUILayout.LabelField(take.ToString() + " : ");
			if (GUILayout.Button("재생"))
			{
				if (take == EMassTake.enterstage01 || take == EMassTake.down01)
					controller.Manager.StopTake();

				controller.PlayTake(take, actorList, isLoop, area);
				controller.EventTakeEnd += PlayCameraTake;
			}
			EditorGUILayout.EndHorizontal();
		}

		private void PlayCameraTake(Controller.BaseController controller)
		{
			controller.Manager.PlayNextTake();
			controller.EventTakeEnd -= PlayCameraTake;
		}

		protected override void ShowPhase(BaseManager inst)
		{
			EditorGUILayout.BeginHorizontal();
			ShowLabelField("현재 페이즈 : ");
			GUI.enabled = inst.IsActive;
			if (GUILayout.Button("다음 페이즈"))
			{
				var controller = inst.DataList[ESide.ENEMY].CharacterList[0] as Controller.Boss.BaseBossController;

				controller.ChangePhase(inst.Phase + 1);
			}
			GUI.enabled = true;
			EditorGUILayout.EndHorizontal();
		}

		protected override void ShowBossId(BaseManager inst, BaseManager.CheatData cheat)
		{
			ShowEnableMenu(ref cheat.isBossIdForce, "보스 아이디 강제 설정", () =>
			{
				GUI.enabled = true;

				cheat.bossId = EditorGUILayout.IntField("보스 아이디", cheat.bossId);
			});
		}
	}
}
